package com.xiaochen.baidu.service.node;

import com.xiaochen.baidu.common.design.strategy.AbstractMultiThreadStrategyRouter;
import com.xiaochen.baidu.common.design.strategy.StrategyHandler;
import com.xiaochen.baidu.model.entity.RequestEntity;
import com.xiaochen.baidu.service.factory.DefaultStrategyFactory;
import com.xiaochen.baidu.service.thread.LoginThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author xc
 * @date 2025/10/11/20:32
 * @desc
 */
@Slf4j
@Service
public class LoginNode extends AbstractMultiThreadStrategyRouter<RequestEntity, DefaultStrategyFactory.StrategyDynamicContext, String> {
    
    @Resource
    private ConfirmNode confirmNode;
    
    @Override
    protected void multiThread(RequestEntity requestParameter, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        LoginThreadTask loginThreadTask = new LoginThreadTask(requestParameter.getUsername(),requestParameter.getPassword(), moiuLoginService);
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(loginThreadTask,threadPoolExecutor);
        String result = completableFuture.join();
        strategyDynamicContext.setOauthSession(result);
        log.info("获取outhSession:{}",result);
    }

    @Override
    protected String apply(RequestEntity requestParameter, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        return route(requestParameter, strategyDynamicContext);
    }

    @Override
    public StrategyHandler<RequestEntity, DefaultStrategyFactory.StrategyDynamicContext, String> map(RequestEntity requestEntity, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        return confirmNode;
    }
}
